package com.zy.JSON;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.Domain.Person;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JsonTestOne
{
    @Test
    public void Test1() throws IOException
    {
        //1、定义Java对象
        Person person=new Person();
        person.setName("张三");
        person.setAge(23);
        person.setGender("男");

        //2、获取ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();

        //3、Java对象转换成JSON,以字符串形式输出
        String s = objectMapper.writeValueAsString(person);
        System.out.println(s);

        //4、Java对象转换成Json，输出到文件中
        objectMapper.writeValue(new File("D://json1.txt"),person);
        //输出到文件中：{"name":"张三","age":23,"gender":"男"}

        //5、Java对象转换成Json，关联到字符输出流
        objectMapper.writeValue(new FileWriter("D://json2.txt"),person);

    }

    @Test
    public void Test2() throws JsonProcessingException
    {
        //1、定义Java对象
        Person person=new Person();
        person.setName("张三");
        person.setAge(23);
        person.setGender("男");
        person.setBirthday(new Date());

        //2、获取ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();

        //3、Java对象转换成JSON，以字符串形式输出
        String s = objectMapper.writeValueAsString(person);
        System.out.println(s);
        //{"name":"张三","age":23,"gender":"男","birthday":1609991325097},日期输出格式需转换
        //使用@JsonIgnore注解忽略属性后：{"name":"张三","age":23,"gender":"男"}
        // 使用@JsonFormat(pattern = "yyyy-MM-dd")注解设置格式：{"name":"张三","age":23,"gender":"男","birthday":"2021-01-07"}
    }

    @Test
    public void Test3() throws JsonProcessingException
    {
        //1、定义Java对象
        Person p1=new Person();
        p1.setName("张三");
        p1.setAge(23);
        p1.setGender("男");
        p1.setBirthday(new Date());

        Person p2=new Person();
        p2.setName("李四");
        p2.setAge(23);
        p2.setGender("男");
        p2.setBirthday(new Date());

        Person p3=new Person();
        p3.setName("王五");
        p3.setAge(23);
        p3.setGender("男");
        p3.setBirthday(new Date());

        //复杂的Java对象
        ArrayList<Person> ps=new ArrayList<>();
        ps.add(p1);
        ps.add(p2);
        ps.add(p3);
        //2、获取ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();

        //3、Java对象转换成JSON，以字符串形式输出
        String s = objectMapper.writeValueAsString(ps);
        System.out.println(s);
        /*
            [{"name":"张三","age":23,"gender":"男","birthday":"2021-01-07"},
            {"name":"李四","age":23,"gender":"男","birthday":"2021-01-07"},
            {"name":"王五","age":23,"gender":"男","birthday":"2021-01-07"}]
        */
    }

    @Test
    public void Test4() throws JsonProcessingException
    {
        //1、定义一个Map对象
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name","张三");
        map.put("age",23);
        map.put("gender","男");

        //2、获取ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();

        //3、Java对象转换成JSON，以字符串形式输出
        String s = objectMapper.writeValueAsString(map);
        System.out.println(s);
        //{"gender":"男","name":"张三","age":23}
    }

    @Test
    public void Test5() throws IOException
    {
        //1、初始化JSON字符串
        String json="{\"name\":\"张三\",\"age\":23,\"gender\":\"男\",\"birthday\":\"2021-01-07\"}";

        //2、获取ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();

        //3、JSON对象转换成Java，以字符串形式输出。参数：JSON格式字符串，Java对象的class
        Person person = objectMapper.readValue(json, Person.class);
        System.out.println(person);
        //{"gender":"男","name":"张三","age":23}
    }
}
